// 更改 Vuex 的 store 中的状态的唯一方法是提交 mutation
// this.$store.commit(method, params)

import store from '../'
import $ from 'jquery'
import Vue from 'vue'
import XLocation from "../../x/XLocation";

export default {
    updateLogin: (state, payload) => {
        if (payload != null) {
            state.user.userId = payload
            state.user.isLogin = true
        } else {
            state.user.userId = null
            state.user.isLogin = false
        }
    },
    updateShareUrlKey: (state, shareUrlKey) => {
        if (shareUrlKey != null) {
            state.shareUrlKey = shareUrlKey
        } else {
            state.shareUrlKey = 'traceShareId'
        }
    },
    updateCurrentUser (state, user) {
        /*用户信息*/
        let currentUser = this.getters.currentUserFn || {};
        try {
            state.currentUser = Object.assign({}, currentUser, user);
        } catch (e) {
            state.currentUser = user
        }
        window.localStorage.setItem(
            Vue.prototype.CURRENTUSER,
            window.encodeURIComponent(JSON.stringify(state.currentUser))
        )
    },
    /*更新当前店铺*/
    updateCurrentStore (state, val) {
        /*店铺位置信息*/
        try {
            state.currentStore = Object.assign({}, state.currentStore, val)
        } catch (e) {
            state.currentStore = val;
        }
        window.sessionStorage.setItem(
            Vue.prototype.CURRENTSTORE,
            window.encodeURIComponent(JSON.stringify(state.currentStore))
        )
    },
    /*更新当前店铺和用户定位距离*/
    currentUserStoreDiscount (state, val) {
        let currentUser = this.getters.currentUserFn;
        let currentThird = this.getters.currentThirdFn;
        let currentStore = this.getters.currentStoreFn;
        if (currentStore && (currentStore.latitude || currentStore.longitude)) {
            if (currentUser && (currentUser.latitude || currentUser.longitude)) {
                state.userStoreDiscount = XLocation.countDistance(currentUser.latitude, currentUser.longitude, currentStore.latitude, currentStore.longitude);
                if (state.userStoreDiscount) {
                    state.userStoreDiscount = state.userStoreDiscount > 1000 ? parseInt(state.userStoreDiscount / 10) / 100 + 'km' : parseInt(state.userStoreDiscount) + 'm'
                }
                window.sessionStorage.setItem('userStoreDiscount', JSON.stringify(state.userStoreDiscount));
            } else if (currentThird && (currentThird.latitude || currentThird.longitude)) {
                state.userStoreDiscount = XLocation.countDistance(currentThird.latitude, currentThird.longitude, currentStore.latitude, currentStore.longitude);
                if (state.userStoreDiscount) {
                    state.userStoreDiscount = state.userStoreDiscount > 1000 ? parseInt(state.userStoreDiscount / 10) / 100 + 'km' : parseInt(state.userStoreDiscount) + 'm'
                }
                window.sessionStorage.setItem('userStoreDiscount', JSON.stringify(state.userStoreDiscount));
            }
        }
    },
    updateCurrentOauthThirdId (state, oauthThirdId) {
        let oauthThirdIdDB = window.localStorage.getItem(Vue.prototype.OAUTHTHIRDID)
        if (
            oauthThirdIdDB == null ||
            !!oauthThirdIdDB ||
            oauthThirdIdDB != oauthThirdId
        ) {
            let thirdPoolJson = window.localStorage.getItem(
                Vue.prototype.OAUTHTHIRDPOOL
            )
            let thirdPool = JSON.parse(window.decodeURIComponent(thirdPoolJson))
            thirdPool = thirdPool && thirdPool != 'undefined' ? thirdPool : []
            let currentThird
            thirdPool.forEach(function (t) {
                if (t.oauthThirdId == oauthThirdId) {
                    currentThird = t
                }
            })
            if (!!currentThird) {
                state.currentThird = currentThird
                let third4Encode = window.encodeURIComponent(
                    JSON.stringify(currentThird)
                )
                window.localStorage.setItem(Vue.prototype.CURRENTTHIRD, third4Encode)
                Cookie.setCookie(Vue.prototype.CURRENTTHIRD, third4Encode, 100)
            } else {
                state.currentThird = null
                window.localStorage.removeItem(Vue.prototype.CURRENTTHIRD)
                Cookie.setCookie(Vue.prototype.CURRENTTHIRD, null, 100)
            }
            window.localStorage.setItem(Vue.prototype.OAUTHTHIRDID, oauthThirdId)
        }
    },
    updateCurrentThird (state, third) {
        let currentThrid = this.getters.currentThirdFn || {};
        try {
            state.currentThird = Object.assign({}, currentThrid, third);
        } catch (e) {
            state.currentThird = third;
        }
        let third4Encode = window.encodeURIComponent(JSON.stringify(state.currentThird));
        let third4EncodeString = JSON.stringify(state.currentThird);
        window.localStorage.setItem(Vue.prototype.CURRENTTHIRD, third4Encode);
        /*这里存储cookie的时候不能用encodeURIComponent编码，只能存储string类型,*/
        /*报错，暂时注释*/
        // Cookie.setCookie(Vue.prototype.CURRENTTHIRD, third4EncodeString, 100);
        //更新认证第三方池
        let thirdPoolJson = window.localStorage.getItem(
            Vue.prototype.OAUTHTHIRDPOOL
        );
        let thirdPool = JSON.parse(window.decodeURIComponent(thirdPoolJson));
        if (thirdPool == null) {
            thirdPool = new Array()
        }
        if (!!third) {
            thirdPool.forEach(function (t) {
                if (t.oauthThirdId == state.currentThird.oauthThirdId) {
                    thirdPool.splice(t)
                }
            })
            thirdPool.push(state.currentThird)
            window.localStorage.setItem(
                Vue.prototype.OAUTHTHIRDPOOL,
                window.encodeURIComponent(JSON.stringify(thirdPool))
            )
        }
    },
    updateNextPage (state, nextPage) {
        state.nextPage = nextPage
        window.localStorage.setItem(Vue.prototype.NEXTPAGE, nextPage)
    },
    loginOut (state) {
        state.currentUser = {};
        Vue.prototype.userId = null;
        window.localStorage.setItem(Vue.prototype.CURRENTUSER, null)
    },
    updateShareInfo (state, shareInfo) {
        state.shareInfo = shareInfo
        window.sessionStorage.setItem(
            Vue.prototype.SHAREINFO,
            window.encodeURIComponent(JSON.stringify(shareInfo))
        )
    },
    updateBrowser (state, browser) {
        state.browser = browser
        window.localStorage.setItem(
            Vue.prototype.BROWSER,
            window.encodeURIComponent(JSON.stringify(browser))
        )
    },
    updateCookieUser (state, cookieUser) {
        state.cookieUser = cookieUser
        window.localStorage.setItem(Vue.prototype.COOKIEUSER, cookieUser)
    },
    /*由于每个轮播图滑动的时候都会改变shareBody所以在这里存一份Id（媒体id，推广id，实景id等等，统一存在这里）*/
    updateSessionNowId (state, nowId) {
        state.nowId = nowId
        window.sessionStorage.setItem(Vue.prototype.NOWID, nowId)
    },
    // 红包参数
    getredEnvelopeData (state, redEnvelopeDataVal) {
        state.redEnvelopeData = redEnvelopeDataVal
    },
    // 红包参数数组
    getredEnvelopeList (state, redVal) {
        state.redEnvelopeList = [...redVal]
    },
    // 视频进度条
    setTimeAll (state, TimeAllVal) {
        state.timeAll = TimeAllVal || 0
        // console.log(TimeAllVal, '视频进度条');

    },
    // 视频总时长
    setTimeTotal (state, timeTotalVal) {
        // console.log(timeTotalVal, '视频总时长');
        state.timeTotal = timeTotalVal
    },
    // 红包弹窗的状态
    setredAlert (state, redAlert) {
        // console.log(redAlert, '红包状态');

        state.redAlert = redAlert
    },
    //商品详情购买弹窗跟侧边分类弹窗两个弹窗的状态
    setShowSlide (state, showSlide) {
        state.showSlide = showSlide
    },

}
